//package com.cloudz.iptv.http;
//
//import android.content.Context;
//import android.support.annotation.NonNull;
//
//import com.alibaba.fastjson.JSONObject;
//import com.cloudz.iptv.api.v1.UpdateService;
//import com.cloudz.iptv.utils.InPutJson;
//import com.cloudz.iptv.utils.ResponseUtils;
//import com.cloudz.iptv.utils.RetrofitUtils;
//import com.cloudz.iptv.utils.xupdate.HProgressDialogUtils;
//import com.hjq.toast.ToastUtils;
//import com.xuexiang.xupdate.XUpdate;
//import com.xuexiang.xupdate.proxy.IUpdateHttpService;
//import com.xuexiang.xupdate.service.OnFileDownloadListener;
//
//import java.io.File;
//import java.util.Map;
//
//import okhttp3.ResponseBody;
//import retrofit2.Call;
//import retrofit2.Response;
//
//
///**
// * @File Name: com.cloudz.iptv.http
// * @Author: YJ //作者及
// * @Date: 2019/1/13 10:46 PM//完成日期
// * @Description: // 描述
// * @Version: v0.0.1 // 版本信息
// * @Function List: // 主要函数及其功能
// * @Others: // 其它内容的说明
// * @History: // 历史修改记录
// */
//public class OKHttpUpdateHttpService implements IUpdateHttpService
//{
//
//    public Context context;
//
//    public OKHttpUpdateHttpService(Context context)
//    {
//        this.context = context;
//    }
//
//    @Override
//    public void asyncGet(@NonNull String url, @NonNull Map<String, Object> params, final @NonNull Callback callBack)
//    {
//        // 验证绑定情况
//        UpdateService updateService = RetrofitUtils.getInstance().createApi(url + "/iptv/res/", UpdateService.class);
//
//        updateService.update(params.get("deviceId") + "", params.get("versionCode") + "").enqueue(new retrofit2.Callback<ResponseBody>()
//        {
//
//            @Override
//            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
//            {
//                try
//                {
//                    InPutJson inOutJson = ResponseUtils.getInOutJson(response);
//
//                    if (inOutJson.result)
//                    {
//                        JSONObject a = JSONObject.parseObject(inOutJson.data);
//
//                        callBack.onSuccess(a.toJSONString());
//                    }
//                } catch (Exception e)
//                {
//                    e.printStackTrace();
//                }
//
//            }
//
//            @Override
//            public void onFailure(Call<ResponseBody> call, Throwable t)
//            {
//                callBack.onError(t);
//            }
//        });
//
//    }
//
//    @Override
//    public void asyncPost(@NonNull String url, @NonNull Map<String, Object> params, @NonNull Callback callBack)
//    {
//
//    }
//
//    @Override
//    public void download(@NonNull String url, @NonNull final String path, @NonNull String fileName, @NonNull final DownloadCallback callback)
//    {
//
//        XUpdate.newBuild(context)
//                .apkCacheDir(path) //设置下载缓存的根目录
//                .build()
//                .download(url, new OnFileDownloadListener()
//                {   //设置下载的地址和下载的监听
//                    @Override
//                    public void onStart()
//                    {
//                        HProgressDialogUtils.showHorizontalProgressDialog(context, "下载进度", false);
//                    }
//
//                    @Override
//                    public void onProgress(float progress, long total)
//                    {
//                        HProgressDialogUtils.setProgress(Math.round(progress * 100));
//                    }
//
//                    @Override
//                    public boolean onCompleted(File file)
//                    {
//                        HProgressDialogUtils.cancel();
//                        ToastUtils.show("apk下载完毕，文件路径：" + file.getPath());
//                        return false;
//                    }
//
//                    @Override
//                    public void onError(Throwable throwable)
//                    {
//                        HProgressDialogUtils.cancel();
//                    }
//                });
//
////        try
////        {
////            DownLoadFileServiceApi downLoadFileServiceApi = RetrofitUtils.getInstance().createApi("http://192.168.2.23:8089", DownLoadFileServiceApi.class);
////
////            downLoadFileServiceApi.downloadApk(url).enqueue(new retrofit2.Callback<ResponseBody>()
////            {
////                @Override
////                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
////                {
////                    callback.onStart();
////
////                    File file = new File(path + "/update.apk");
////
////                    try
////                    {
////                        File parentFile = file.getParentFile();
////                        if (!parentFile.exists())
////                        {
////                            parentFile.mkdirs();
////                        }
////                        file.getParentFile();
////
////                        FileUtils.writeByteArrayToFile(file, response.body().bytes());
////                    } catch (Exception e)
////                    {
////                        e.printStackTrace();
////                    }
////                    callback.onSuccess(file);
////                }
////
////                @Override
////                public void onFailure(Call<ResponseBody> call, Throwable t)
////                {
////
////                }
////            });
////        } catch (Exception e)
////        {
////            e.printStackTrace();
////        }
//
//
////        downLoadFileServiceApi.downloadApk(url).enqueue(new RetrofitCallback<ResponseBody>()
////        {
////            @Override
////            public void onSuccess(Call<ResponseBody> call, Response<ResponseBody> response)
////            {
////
////                try
////                {
////                    callback.onStart();
////
////                    InputStream is = response.body().byteStream();
////                    //获取文件总长度
////                    long totalLength = is.available();
////
////                    File file = new File(path, "update.apk");
////                    FileOutputStream fos = new FileOutputStream(file);
////                    BufferedInputStream bis = new BufferedInputStream(is);
////                    byte[] buffer = new byte[1024];
////                    int len;
////                    while ((len = bis.read(buffer)) != -1) {
////                        fos.write(buffer, 0, len);
////                        //此处进行更新操作
////                        //len即可理解为已下载的字节数
////                        callback.onProgress(len, totalLength);
////                    }
////                    fos.flush();
////                    fos.close();
////                    bis.close();
////                    is.close();
////
////                    callback.onSuccess(file);
////                } catch (Exception e)
////                {
////                    e.printStackTrace();
////                }
////
////            }
////
////            @Override
////            public void onFailure(Call<ResponseBody> call, Throwable t)
////            {
////
////            }
////
////            @Override
////            public void onLoading(long total, long progress)
////            {
////                super.onLoading(total, progress);
////            }
////        });
//
//    }
//
//    @Override
//    public void cancelDownload(@NonNull String url)
//    {
//
//    }
//}
